/*
 * Duplicate Detector Copyright (C) 2010 Marco Biscaro <marcobiscaro2112@gmail.com>
 * 
 * This file is part of Duplicate Detector.
 *
 * Duplicate Detector is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * Duplicate Detector is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with Duplicate Detector.  If not, see <http://www.gnu.org/licenses/>.
 */
package com.googlecode.duplicatedetector.model;

import java.io.File;
import java.util.ArrayList;
import java.util.List;

import javax.swing.AbstractListModel;

import com.googlecode.duplicatedetector.util.Logger;

/**
 * List that carry all directories that will be included in the search.
 * 
 * @author Marco Biscaro
 */
public class FileListModel extends AbstractListModel {

	private static final long serialVersionUID = -456210025175399517L;
	private static final Logger LOGGER = new Logger(FileListModel.class);
	private List<File> files;

	public FileListModel() {
		files = new ArrayList<File>();
	}

	@Override
	public Object getElementAt(int index) {
		return files.get(index);
	}

	@Override
	public int getSize() {
		return files.size();
	}

	public void add(File file) {
		if (files.contains(file)) {
			return;
		}
		files.add(file);
		LOGGER.debug("File added: " + file);
		fireIntervalAdded(this, getSize(), getSize());
	}

	public void remove(int index) {
		LOGGER.debug("File removed: " + files.remove(index));
		fireIntervalRemoved(this, index, index);
	}

	public int indexOf(File file) {
		return files.indexOf(file);
	}

	public void clear() {
		int size = files.size();
		files.clear();
		LOGGER.debug("File list cleared");
		fireIntervalRemoved(this, 0, size);
	}

	public List<File> getFiles() {
		return new ArrayList<File>(files);
	}

}
